package com.course.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.course.domain.MyClass;
import com.course.domain.Student;
import com.course.domain.Teacher;
import com.course.domain.User;
import com.course.hibernate.HibernateSessionFactory;

public class UserDao extends SupperDao
{

	@SuppressWarnings( "unchecked" )
	public User getUserByUsername( String username )
	{
		String hql = "from User where username = '" + username + "'";
		List<User> list = this.findByHQL( hql );
		if(list.isEmpty())
			return null;
		
		return list.get( 0 );
	}

	public boolean updatePwb( Integer id, String newPassword )
	{
		String hql = "UPDATE User SET password = ? where id = ?";
		Object param[] = { newPassword, id };
		if(this.executeUpdate( hql, param ) == 0 )
			return false;
		return true;
	}

	@SuppressWarnings( "unchecked" )
	public Map getStudentInfo( Integer id )
	{
		Map info = new HashMap();
		try
		{
			session = HibernateSessionFactory.getSession(); // 获取Session对象
			session.beginTransaction(); // 开启事物
			
			Student stu = ( Student )session.get( Student.class, id );
			MyClass myClass = ( MyClass )session.get( MyClass.class, stu.getMyClass().getId() );
			info.put( "student", stu );
			info.put( "classes", myClass );
			info.put( "specialty", myClass.getSpecialty().getName() );
			info.put( "department", myClass.getSpecialty().getDepartment().getName() );
			info.put( "college", myClass.getSpecialty().getDepartment().getCollege().getName() );
			session.getTransaction().commit(); // 提交事物
		} catch ( Exception e )
		{
			e.printStackTrace(); // 打印异常信息
			session.getTransaction().rollback(); // 回滚事物
		} finally
		{
			session.close();
		}
		return info;
	}

	@SuppressWarnings( "unchecked" )
	public Map getTeacherInfo( Integer id )
	{
		Map info = new HashMap();
		try
		{
			session = HibernateSessionFactory.getSession(); // 获取Session对象
			session.beginTransaction(); // 开启事物
			
			Teacher teacher = ( Teacher )session.get( Teacher.class, id );
			info.put( "teacher", teacher );
			info.put( "department", teacher.getDepartment().getName() );
			info.put( "college", teacher.getDepartment().getCollege().getName() );
			
			session.getTransaction().commit(); // 提交事物
		} catch ( Exception e )
		{
			e.printStackTrace(); // 打印异常信息
			session.getTransaction().rollback(); // 回滚事物
		} finally
		{
			session.close();
		}
		return info;
	}

	@SuppressWarnings( "unchecked" )
	public List< Object[] > getStudentsByCourseId( int courseId )
	{
		String hql = "select s, mc.name from Student s, MyClass mc, StuCour sc where s.id = sc.id.student.id and s.myClass.id = mc.id and sc.id.course.id = " + courseId;
		return this.findByHQL( hql );
	}

	@SuppressWarnings( "unchecked" )
	public long countOfStudentByMyClassId( int myClassId )
	{
		String hql = "select count(*) from Student where myClass.id = " + myClassId;
		List<Long> list = this.findByHQL( hql );
		return list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public List< Student > getStudents( int start, int length, int myClassId )
	{
		String hql = "from Student s where s.myClass.id = " + myClassId;
		return this.findPaging( hql, start, length, null );
	}

	public Student getStudent( int studentId )
	{
		return ( Student )this.get( Student.class, studentId );
	}

	public boolean deleteStudentById( Integer id )
	{
		String hql = "delete from StuCour sc where sc.id.student.id = " + id;
		Student student = null;
		try
		{
			session = HibernateSessionFactory.getSession(); // 获取Session对象
			session.beginTransaction(); // 开启事物
			session.createQuery( hql ) // 创建Query对象
					.executeUpdate(); // 更新
			student = ( Student )session.get( Student.class, id );
			session.delete( student );
			session.getTransaction().commit(); // 提交事物
			session.close();
		} catch ( Exception e )
		{
			e.printStackTrace(); // 打印异常信息
			session.getTransaction().rollback(); // 回滚事物
			session.close();
			return false;
		}
		return true;
	}

	@SuppressWarnings( "unchecked" )
	public long countOfTeacher( int departmentId )
	{
		String hql = "select count(*) from Teacher where department.id = " + departmentId;
		List<Long> list = this.findByHQL( hql );
		return list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public List< Teacher > getTeachers( int start, int length, int departmentId )
	{
		String hql = "from Teacher t where t.department.id = " + departmentId;
		return this.findPaging( hql, start, length, null );
	}

	public boolean deleteTeacherById( Integer id )
	{
		Teacher teacher = ( Teacher )this.get( Teacher.class, id );
		return this.delete( teacher );
	}

	@SuppressWarnings( "unchecked" )
	public Teacher getTeacherByCourseId( int courseId )
	{
		List list = null;
		String hql = "select t from Teacher t, Course c where c.teacher.id = t.id and c.id = " + courseId;
		list = ( List )this.findByHQL( hql );
		if(list.isEmpty())
			return null;
		return ( Teacher )list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public List< Teacher > getTeachersByDepartmentId( Integer departmentId )
	{
		String hql = "from Teacher t where t.department.id = " + departmentId;
		return this.findByHQL( hql );
	}

}
